# modified version from mongrel
$:.unshift File.join(File.dirname(__FILE__), "..", "..", "lib")
require 'esi/handler'
require 'test/unit'
SERVER_ROOT = "#{File.dirname(__FILE__)}/../../"
ENV["test"] = "true"
require "#{File.dirname(__FILE__)}/../../lib/log/logger"
DOCROOT = "#{File.dirname(__FILE__)}/docs/"

def redirect_test_io
  orig_err = STDERR.dup
  orig_out = STDOUT.dup
  STDERR.reopen("test_stderr.log")
  STDOUT.reopen("test_stdout.log")

  begin
    yield
  ensure
    STDERR.reopen(orig_err)
    STDOUT.reopen(orig_out)
  end
end


# Either takes a string to do a get request against, or a tuple of [URI, HTTP] where
# HTTP is some kind of Net::HTTP request object (POST, HEAD, etc.)
def hit(uris)
  results = []
  uris.each do |u|
    res = nil

    assert_nothing_raised do
      if u.kind_of? String
        res = Net::HTTP.get(URI.parse(u))
      else
        url = URI.parse(u[0])
        res = Net::HTTP.new(url.host, url.port).start {|h| h.request(u[1]) }
      end
    end

    assert_not_nil res, "Didn't get a response: #{u}"
    results << res
  end

  return results
end

def check_status(results, expecting)
  results.each do |res|
    assert(res.kind_of?(expecting), "Didn't get #{expecting}, got: #{res.class}")
  end
end


class MockRequest
  def initialize(params={})
    @params = params
  end
  def params
    @params
  end
end

def cached?( uri )
  $esi_handler.parser.cache.get( uri, {} )
end
